﻿using InkjetControl.codec;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace InkjetControl.services.resolvers
{
    public sealed class MessageResolverFactory
    {

        //private const List<IResolver> mServerResolvers = new SynchronizedCollection();
        //private const List<IResolver> mClientResolvers = new CopyOnWriteArrayList<>();
        private static readonly SynchronizedCollection<IResolver> mServerResolvers = new SynchronizedCollection<IResolver>();
        private static readonly SynchronizedCollection<IResolver> mClientResolvers = new SynchronizedCollection<IResolver>();

        private static MessageResolverFactory _instance;
        // 定义一个标识确保线程同步
        private static readonly object locker = new object();


        internal MessageResolverFactory()
        {

        }

        public static MessageResolverFactory GetInstance()
        {
            if (_instance == null)
            {
                lock (locker)
                {
                    // 如果类的实例不存在则创建，否则直接返回
                    if (_instance == null)
                    {
                        _instance = new MessageResolverFactory();
                    }
                }
            }
            return _instance;
        }

        public void RegisterResolver(IResolver resolver, bool isSrv)
        {
            if (isSrv)
            {
                if (!mServerResolvers.Contains(resolver))
                {
                    mServerResolvers.Add(resolver);
                }
            }
            else
            {
                if (!mClientResolvers.Contains(resolver))
                {
                    mClientResolvers.Add(resolver);
                }
            }
        }
        public void UnregisterResolver(IResolver resolver, bool isSrv)
        {
            if (isSrv)
            {
                _ = mServerResolvers.Remove(resolver);
            }
            else
            {
                _ = mClientResolvers.Remove(resolver);
            }
        }

        public void UnRegisterResolvers(bool isSrv)
        {
            if (isSrv)
            {
                mServerResolvers.Clear();
            }
            else
            {
                mClientResolvers.Clear();
            }
        }

        public IResolver GetMessageResolver(Message message, bool isSrv)
        {
            if (isSrv)
            {
                foreach(IResolver resolver in mServerResolvers)
                {
                    if (resolver.Support(message))
                    {
                        return resolver;
                    }
                }
            }else
            {
                foreach (IResolver resolver in mClientResolvers)
                {
                    if (resolver.Support(message))
                    {
                        return resolver;
                    }
                }
            }

            throw new Exception("cannot find resolver, message type: " + message.MessageType);
        }
    }
}
